package collection;

import java.util.Arrays;
import java.util.List;

//数组转为集合
public class ArrayToList {
    public static void main(String[] args) {
        String[] array = {"one","two","three","four","five"};
        System.out.println("array:"+ Arrays.toString(array));//[one,two,three,four,five]

        //asList()方法会返回内部类的ArrayList，内部直接引用给定数组array
        List<String> list = Arrays.asList(array);
        System.out.println("lost:"+list);

        //对数组操作后，集合也会相应改变
        array[1] = "six";
        System.out.println("array:"+ Arrays.toString(array));
        System.out.println("list:"+list);

        //对集合操作后，数组也会做相应的改变
        list.set(2,"seven");
        System.out.println("array:"+ Arrays.toString(array));
        System.out.println("list:"+list);

        //list.add("!!!");    各集合添加元素或删除元素时相当于给数组添加或删除元素
        //而数组是定长的，不会自动扩容或缩容，因此会发生异常
    }
}
